package com.towexin.application.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.towexin.application.system.domain.Menu;
import com.towexin.application.system.domain.SystemSetting;
import com.towexin.application.system.mapper.MenuMapper;
import com.towexin.application.system.mapper.SystemSettingMapper;
import com.towexin.application.system.service.SystemSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author Towexin
 * @version 1.0
 * @date 2021/4/27 16:36
 */
@Service
public class SystemSettingServiceImpl extends ServiceImpl<SystemSettingMapper, SystemSetting> implements SystemSettingService {
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private Environment environment;

    @Override
    public Map<String, Object> getSystemConfig() {
        Menu menu = menuMapper.selectSystemHome();
        SystemSetting systemSetting = getById("system-setting");

        Map<String, Object> map = new HashMap<>();

        // logo
        Map<String, Object> logoMap = new HashMap<>();
        logoMap.put("title", systemSetting.getSystemName());
        logoMap.put("image", "/file/" + systemSetting.getSystemLogo());

        // menu
        Map<String, Object> menuMap = new HashMap<>();
        menuMap.put("data", environment.getProperty("menu.data"));
        menuMap.put("accordion", systemSetting.getMenuAccordion());		// 是否同时只打开一个菜单目录
        menuMap.put("method", environment.getProperty("menu.method"));		// 请求方式
        menuMap.put("control", systemSetting.getMenuControl());		// 是否开启多系统菜单模式
        menuMap.put("async", environment.getProperty("menu.async"));		// 渲染模式，true 为异步接口的方式, false 为静态数据
        menuMap.put("select", null);		// 默认选中的菜单项
        Map<String, Object> fieldMap = new HashMap<>();
        fieldMap.put("children", environment.getProperty("menu.fields.children"));
        fieldMap.put("_iframe", environment.getProperty("menu.fields.openType._iframe"));
        menuMap.put("fields", fieldMap);

        // tab
        Map<String, Object> tabMap = new HashMap<>();
        tabMap.put("muiltTab", systemSetting.getMuiltTab());	// 是否开启多选项卡
        tabMap.put("keepState", systemSetting.getTabKeepState());	// 选项卡切换时，是否刷新页面
        tabMap.put("session", systemSetting.getTabSession());	// 存储记忆，刷新浏览器时是否保持打开 Tab
        tabMap.put("tabMax", systemSetting.getTabMax());				// 最大打开标签页数量
        Map<String, Object> indexMap = new HashMap<>();
        indexMap.put("title", "后台首页");
        indexMap.put("href", "page/err/404");
        indexMap.put("id", null);
        if (menu!=null){
            indexMap.put("title", menu.getMenuName());
            indexMap.put("href", menu.getMenuHref());
            indexMap.put("id", menu.getId());
        }
        tabMap.put("index", indexMap);

        // 主题
        Map<String, Object> themeMap = new HashMap<>();
        themeMap.put("defaultColor", systemSetting.getThemeDefaultColor());		// 默认主题色，对应 colors 配置中的 ID 标识
        themeMap.put("defaultMenu", systemSetting.getThemeDefaultMenu());		// 默认的菜单主题 dark-theme 黑 / light-theme 白
        themeMap.put("allowCustom", systemSetting.getThemeAllowCustom());		// 是否允许用户切换主题，false 时关闭自定义主题面板

        // 主题颜色
        //List<Map<String, String>> colors = new ArrayList<>();
        //String[] color = {"#2d8cf0", "#36b368", "#f6ad55", "#f56c6c", "#3963bc"};
        //for (int i = 0; i < color.length; i++){
        //    Map<String, String> colorMap = new HashMap<>();
        //    colorMap.put("id", String.valueOf(i+1));
        //    colorMap.put("color", color[i]);
        //    colors.add(colorMap);
        //}

        // 其他配置
        Map<String, Object> otherMap = new HashMap<>();
        otherMap.put("keepLoad", systemSetting.getOtherKeepLoad());		// 主页动画时长
        otherMap.put("autoHead", systemSetting.getOtherAutoHead());		// 布局顶部主题

        // 头部配置
        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put("message", false);		// 站内消息，数据来源，通过 false 设置关闭

        map.put("logo", logoMap);
        map.put("menu", menuMap);
        map.put("tab", tabMap);
        map.put("theme", themeMap);
        map.put("colors", JSON.parseArray(systemSetting.getColorJsonData()));
        map.put("other", otherMap);
        map.put("header", headerMap);

        return map;
    }
}
